<?php
/**
 * Copyright 2014 TinPHP Systems, Inc.
 * @package TinPhp
 */
/** 日期时间处理类 */
class TinDateTime
{

  public static function now()
  {
    return getdate();
  }

  public static function nowAsString($format='Y-m-d H:i:s')
  {
    return date($format);
  }

  public static function nowTimeStamp()
  {
    return time();
  }

  public static function future($ts)
  {
    return getdate(TinDateTime::futureTimeStamp($ts));
  }

  public static function futureAsString($ts, $format='Y-m-d H:i:s')
  {
    return date($format, TinDateTime::futureTimeStamp($ts));
  }

  public static function futureTimeStamp($ts)
  {
    return TinDateTime::nowTimeStamp() + $ts;
  } 
  /**
   * timestr 格式必须为  2009-07-02 19:31:49
   **/
  public static function toISO8601($timestr){
    
    $hour=substr($timestr,11,2);
    $minute=substr($timestr,14,2);
    $second=substr($timestr,17,2);
    $year=substr($timestr,0,4);
    $month=substr($timestr,5,2);
    $day=substr($timestr,8,2);

    $time=mktime($hour,$minute,$second,$month,$day,$year);
    $time=date('c',$time);
    $tmp=explode("\+", $time);
   	return  str_replace('+'.$tmp[1],'.000Z',$time);
  }
  
  public static function secToStr($sec){
    $str="";
    $h =intval( $sec /( 60 * 60) );
    if(0 <$h ){
      $str .= $h ."时";
      $sec -= $h * 60 * 60 ;
      }
    $m =intval( $sec /( 60 ) );
    if(0 <$m ){
      $str .= $m ."分";
      $sec -= $m *60;
      }
      $str .= $sec ."秒";
      return $str;
    }

    public static function secToArray($sec){
        $arr = array();
        $h =intval( $sec /( 60 * 60) );
        if(0 <$h ){            
            $sec -= $h * 60 * 60 ;
        }
        $m =intval( $sec /( 60 ) );
        if(0 <$m ){
            $sec -= $m *60;
        }
        $arr["hour"] = $h;
        $arr["minute"] = $m;
        $arr["second"] = $sec;
      return $arr;
    }

}
